package controller;

import entity.Advertisement;
import entity.Comment;
import entity.Personnel;
import entity.Role;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import service.ProductService;
import service.UserService;
import util.Page;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;


/**
 * Created by jifeng on 2016/12/2 0002.
 */
@Controller
public class Home {

    @Resource(name = "userService")
    private UserService service;
    @Resource(name = "productService")
    private ProductService productService;
    @RequestMapping(value = "/")
    public String  index(){

        return"index";
    }

    /**
     * Created by jifeng
     * 跳到权限管理菜单
     * */
    @RequestMapping(value = "/rightAdm")
    public Model rightAdm(Model mv){
        List<Role> roleList = service.findRoleList();
        mv.addAttribute("roleList",roleList);
        return mv;
    }

    /**
     * Created by jifeng
     * 权限管理菜单
     * */
    @RequestMapping(value = "/addMenuPermissions")
    public String addMenuPermissions(HttpServletRequest request, HttpServletResponse response,Model mv){
        String role=request.getParameter("role");
        String productListIds = request.getParameter("productListIds");
        String userListIds = request.getParameter("userListIds");
        String orderListIds = request.getParameter("orderListIds");
        String systemListIds = request.getParameter("systemListIds");
        service.updateRole(Integer.valueOf(role),productListIds,userListIds,orderListIds,systemListIds);
        return "redirect:/rightAdm";
    }

    /**
     * Created by jifeng
     * 跳到角色管理菜单
     * */
    @RequestMapping(value = "/roleAdm")
    public Model roleAdm(HttpServletRequest request,HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            if (pageNo == null) {
                pageNo = "1";
            }

            Page page =service.findRoleList(Integer.valueOf(pageNo),10);

            mv.addAttribute("page",page);

            List<Role> roleList = page.getList();
            mv.addAttribute("roleList",roleList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * Created by jifeng
     * 跳到修改密码
     * */
    @RequestMapping(value = "/modifyAdm")
    public Model modifyAdm(HttpServletRequest request,HttpServletResponse response,Model mv){
        String userName = String.valueOf(request.getSession().getAttribute("userName"));
        List<Personnel> personnel = service.findPersonnelByName(userName);
        mv.addAttribute("personnel",personnel.get(0));
        return mv;
    }

    /**
     * Created by jifeng
     * 修改密码
     * */
    @RequestMapping(value = "/updatePassword")
    public String updatePassword(HttpServletRequest request,HttpServletResponse response){
        String userName = String.valueOf(request.getSession().getAttribute("userName"));
        Personnel personnel =service.findPersonnelByName(userName).get(0);
        String password1= request.getParameter("password1");
        service.updatePersonnel(personnel,password1);
        return "redirect:/";
    }

    /**
     * Created by jifeng
     * 人员列表
     * */
    @RequestMapping(value = "/personnelAdm")
    public Model personnelAdm(HttpServletRequest request,HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            if (pageNo == null) {
                pageNo = "1";
            }

            Page page =service.findPersonnelList(Integer.valueOf(pageNo),10);

            mv.addAttribute("page",page);

            List<Personnel> personnelList = page.getList();
            mv.addAttribute("personnelList",personnelList);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return mv;
    }

    /**
     * Created by jifeng
     * 跳转人员,角色添加页面
     * */
    @RequestMapping(value = "/personnelRoleAdd")
    public Model personnelRoleAdd(Model mv){
        List<Role> roleList =service.findRoleList();
        mv.addAttribute("roleList",roleList);
        return mv;
    }


    /**
     * Created by jifeng
     * 添加角色
     * */
    @RequestMapping(value = "/addRole")
    public String addRole(HttpServletRequest request,HttpServletResponse response){
        String roleName = request.getParameter("roleName");
        String describe = request.getParameter("describe");
        Role role = new Role();
        role.setRoleName(roleName);
        role.setDescribe(describe);
        service.addRole(role);
        return "redirect:/roleAdm";
    }


    /**
     * Created by jifeng
     * 添加人员
     * */
    @RequestMapping(value = "/addPersonnel")
    public String addPersonnel(HttpServletRequest request,HttpServletResponse response){
        String userName = request.getParameter("userName");
        String personnelName = request.getParameter("personnelName");
        String jobName = request.getParameter("jobName");
        String role = request.getParameter("role");
        Role role1= service.findRole(Integer.valueOf(role));
        Personnel personnel = new Personnel();
        personnel.setUserName(userName);
        personnel.setPersonnelName(personnelName);
        personnel.setJobName(jobName);
        personnel.setRoleId(Integer.valueOf(role));
        personnel.setRoleName(role1.getRoleName());
        personnel.setPassword("123456");
        service.addPersonnel(personnel);
        return "redirect:/personnelAdm";
    }

    /**
     * Created by jifeng
     * 删除人员personnel
     * */
    @RequestMapping(value = "/deletePersonnel")
    public String deletePersonnel(HttpServletRequest request,HttpServletResponse response){
        String personnelId = request.getParameter("personnelId");
        service.deletePersonnel(Integer.valueOf(personnelId));
        return "redirect:/personnelAdm";
    }

    /**
     * Created by jifeng
     * 删除角色personnel
     * */
    @RequestMapping(value = "/deleteRole")
    public String deleteRole(HttpServletRequest request,HttpServletResponse response){
        String roleId = request.getParameter("roleId");
        List<Personnel> personnelList =service.findPersonnelListById(Integer.valueOf(roleId));
        for (Personnel personnel:personnelList){
            service.deletePersonnel(personnel.getId());
        }
        service.deleteRole(Integer.valueOf(roleId));
        return "redirect:/roleAdm";
    }

    /**
     * Created by jifeng
     * 设置广告位
     * */
    @RequestMapping(value = "/homeBanner")
    public Model homeBanner(HttpServletRequest request,HttpServletResponse response,Model mv){
        List<Advertisement> advertisementList = service.findAdvertisementList();
        mv.addAttribute("advertisementList",advertisementList);
        return mv;
    }

    /**
     * Created by jifeng
     * 跳到编辑页面
     * */
    @RequestMapping(value = "/bannerEdit")
    public Model bannerEdit(HttpServletRequest request,HttpServletResponse response,Model mv){
        String id=request.getParameter("advertisementId");
        List<Advertisement> advertisementList = service.findAdById(Integer.valueOf(id));
        mv.addAttribute("advertisement",advertisementList.get(0));
        mv.addAttribute("id",id);
        return mv;
    }

    /**
     * Created by jifeng
     * 跳到广告位添加页面
     * */
    @RequestMapping("/bannerAdd")
    public Model bannerAdd(HttpServletRequest request,HttpServletResponse response,Model mv){
        return mv;
    }

    /**
     * Created by jifeng
     * 删除广告位
     * */
    @RequestMapping(value = "/deleteAdvertisement")
    public String deleteAdvertisement(HttpServletRequest request,HttpServletResponse response){
        String adId = request.getParameter("adId");
        service.deleteAdvertisement(Integer.valueOf(adId));
        return "redirect:/homeBanner";
    }


    /**
     * Created by jifeng
     * 编辑广告位
     * */
    @RequestMapping(value = "/updateAdvertisement")
    public String updateAdvertisement(HttpServletRequest request,HttpServletResponse response,@RequestParam MultipartFile myFiles)throws IllegalStateException, IOException{
        String fileName=myFiles.getOriginalFilename();
        // 项目在容器中实际发布运行的根路径
        String realPath=request.getSession().getServletContext().getRealPath("/");
        // 自定义的文件名称
        String trueFileName=String.valueOf(System.currentTimeMillis())+fileName;
        // 设置存放图片文件的路径
        String path= realPath+trueFileName;
        String path1 =trueFileName;
        System.out.println("存放图片文件的路径:"+path);
        myFiles.transferTo(new File(path));
        System.out.println("文件成功上传到指定目录下");
        String adName=request.getParameter("advertisementName");
        String url=request.getParameter("bannerLink");
        String id = request.getParameter("id");
        Advertisement advertisement = new Advertisement();
        advertisement.setAdName(adName);
        if (fileName!=null&&fileName!="") {
            advertisement.setPicture(path1);
        }
        advertisement.setUrl(url);
        service.updateAdvertisement(Integer.valueOf(id),advertisement);
        return "redirect:/homeBanner";
    }

    /**
     * Created by jifeng
     * 添加广告位
     * */
    @RequestMapping(value = "/addAdvertisement")
    public String addAdvertisement(HttpServletRequest request, HttpServletResponse response, @RequestParam MultipartFile myFiles)throws IllegalStateException, IOException {
        String fileName=myFiles.getOriginalFilename();
        // 项目在容器中实际发布运行的根路径
        String realPath=request.getSession().getServletContext().getRealPath("/");
        // 自定义的文件名称
        String trueFileName=String.valueOf(System.currentTimeMillis())+fileName;
        // 设置存放图片文件的路径
        String path= realPath+trueFileName;
        String path1 =trueFileName;
        System.out.println("存放图片文件的路径:"+path);
        myFiles.transferTo(new File(path));
        System.out.println("文件成功上传到指定目录下");
        String adName=request.getParameter("advertisementName");
        String url=request.getParameter("bannerLink");
        Advertisement advertisement = new Advertisement();
        advertisement.setAdName(adName);
        advertisement.setPicture(path1);
        advertisement.setUrl(url);
        service.addAdvertisement(advertisement);
        return "redirect:/homeBanner";
    }

    /**
     * Created by jifeng
     * 评价管理
     * */
    @RequestMapping(value = "/commentPanel")
    public Model commentPanel(HttpServletRequest request,HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            String userName = request.getParameter("userName");
            if (pageNo == null) {
                pageNo = "1";
            }

            Page page =productService.commentList(Integer.valueOf(pageNo),userName,10);

            mv.addAttribute("page",page);

            List<Comment> commentList = page.getList();
            mv.addAttribute("commentList",commentList);
            mv.addAttribute("userNames",userName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * Created by jifeng
     * 删除评价
     * */
    @RequestMapping(value = "/deleteComment")
    public String deleteComment(HttpServletRequest request,HttpServletResponse response){
        String ids =request.getParameter("ids");
        productService.deleteComment(ids);
        return "redirect:/commentPanelTrash";
    }


    /**
     * Created by jifeng
     * 删除评价管理
     * */
    @RequestMapping(value = "/commentPanelTrash")
    public Model commentPanelTrash(HttpServletRequest request,HttpServletResponse response,Model mv){
        try {
            String pageNo = request.getParameter("pageNo");
            String userName = request.getParameter("userName");
            if (pageNo == null) {
                pageNo = "1";
            }
            Page page =productService.recoveryCommentList(Integer.valueOf(pageNo),userName,10);

            mv.addAttribute("page",page);
            List<Comment> commentList = page.getList();
            mv.addAttribute("commentList",commentList);
            mv.addAttribute("userNames",userName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }


    /**
     * Created by jifeng
     * 恢复删除评价
     * */
    @RequestMapping(value = "/recoveryComment")
    public String recoveryComment(HttpServletRequest request,HttpServletResponse response){
        String ids =request.getParameter("ids");
        productService.recoveryComment(ids);
        return "redirect:/commentPanel";
    }

}
